package com.ysd.lis.enums;

import com.ysd.lis.entity.DataBaseBo;
import com.ysd.lis.entity.qi.QiDatabase;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.function.BiFunction;

/**
 * @author LuoXS
 * @date 2024/4/26
 * version 1.0
 */
@Getter
@AllArgsConstructor
public enum QiDataSourceEnum {

    ORACLE("0",
            "oracle.jdbc.OracleDriver",
            "jdbc:oracle:thin:@{0}:{1}:{2}", Tools.func),
    SQL_SERVER("1",
            "com.microsoft.sqlserver.jdbc.SQLServerDriver",
            "jdbc:sqlserver://{0}:{1};DatabaseName={2}", Tools.func),
    DM("2",
            "dm.jdbc.driver.DmDriver",
            "jdbc:dm://{0}:{1}/{2}?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8", Tools.func),
    none("3",
            "None",
            "", Tools.func),
    ;


    private final String dataSourceType;
    private final String driverClass;
    private final String dataSourceUrlFormat;
    private final BiFunction<QiDataSourceEnum, QiDatabase, DataBaseBo> func;


    public static QiDataSourceEnum getEnum(QiDatabase calcDatabase) {
        return Arrays.stream(QiDataSourceEnum.values()).filter(ele -> ele.getDataSourceType().equals(calcDatabase.getDbType())).findFirst().orElse(none);
    }


    public static class Tools {
        public static BiFunction<QiDataSourceEnum, QiDatabase, DataBaseBo> func = (x, y) ->
                DataBaseBo.builder()
                        .driverClassName(x.getDriverClass())
                        .url(MessageFormat.format(x.getDataSourceUrlFormat(), y.getDbUrl(), y.getPort(), y.getDbName()))
                        .username(y.getDbUser())
                        .password(y.getDbPass())
                        .build();
    }
}
